package com.ibm.sc.osp.sample.api.controller;

import com.ibm.sc.osp.common.response.RestResponse;
import com.ibm.sc.osp.sample.domain.entity.Message;
import com.ibm.sc.osp.sample.service.MessageService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/message")
public class MessageController {
    
    @Autowired//自动引入bean实例
    private MessageService messageService;
    
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    //接口路径/调用方式配置
    @ResponseBody
    @HystrixCommand
    //对此接口开启hystrix熔断
    public ResponseEntity<RestResponse> add(@RequestBody Message message) {
        try {
            return RestResponse.createSuccessRes(messageService.add(message));
        } catch (Exception e) {
            return RestResponse.createFailRes(e.getMessage());
        }
    }
    
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseBody
    @HystrixCommand
    public ResponseEntity<RestResponse> get(@RequestBody Message message) {
        try {
            return RestResponse.createSuccessRes(messageService.get(message.getId()));
        } catch (Exception e) {
            return RestResponse.createFailRes(e.getMessage());
        }
    }
}
